package com.sihai.ehr.web.action.rank;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

/**
 * @author wxn
 * @since 12-6-24 17:43
 * 职务设置
 */
@Results({
        @Result(name = "list", location = "duty_list.jsp"),
        @Result(name = "add", location = "duty_add.jsp"),
        @Result(name = "main", location = "rank_main.jsp")
})
public class DutyAction extends EHRAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7204414972672240834L;

    @BeanInject
    private DutyService dutyService;

    private LimitedList<Duty> duties;

    private Duty duty;

    private String dutyIds;

    @Action("/rank/duty")
    @Override
    public String execute() throws Exception {
        listAction = "/rank/duty_list.xhtml";
        listTitle = "职务设置";
        return "main";
    }

    @Action("/rank/duty_list")
    public String list() throws Exception {
        if(duty == null){
            duty = new Duty();
        }
        Organization o = new Organization();
        o.setId(organizationId);
        duty.setOrganization(o);
        
        duties = dutyService.search(duty, createLimit(), createSorter());
        
        /* 分页 */
        this.paging(duties);
        /* 设置翻页action */
        this.setPageUrl("rank/duty_list.xhtml");
        
        return "list";
    }

    @Action("/rank/duty_create")
    public String create() throws Exception {
        if(duty == null){
            return execute();
        }

        if(duty.getId() != null && duty.getId() > 0){//edit
            Duty d = dutyService.get(duty.getId());
            if(d != null){
                d.setDutyName(duty.getDutyName());
                d.setDutyContent(duty.getDutyContent());
                dutyService.save(d);
            }
        }else{ //save
            dutyService.save(duty);
        }

        return execute();
    }

    //跳转到添加/编辑页面
    @Action("/rank/duty_add")
    public String add() throws Exception {
        //edit
        if(duty != null && duty.getId() != null){
            duty = dutyService.get(duty.getId());
        }

        return  "add";
    }

    @Action("/rank/duty_delete")
    public String remove() throws Exception {
        if(StringUtils.isBlank(dutyIds)){
            return execute();
        }
        dutyService.remove(dutyIds);
        return execute();
    }

    public LimitedList<Duty> getDuties() {
        return duties;
    }

    public void setDuties(LimitedList<Duty> duties) {
        this.duties = duties;
    }

    public Duty getDuty() {
        return duty;
    }

    public void setDuty(Duty duty) {
        this.duty = duty;
    }

    public String getDutyIds() {
        return dutyIds;
    }

    public void setDutyIds(String dutyIds) {
        this.dutyIds = dutyIds;
    }
}
